package com.ysyc.driver_ee.activity;

import android.app.Dialog;
import android.content.Intent;
import android.graphics.Color;
import android.text.Editable;
import android.text.SpannableString;
import android.text.Spanned;
import android.text.TextPaint;
import android.text.TextWatcher;
import android.text.method.LinkMovementMethod;
import android.text.style.ClickableSpan;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;

import com.google.gson.JsonObject;
import com.ysyc.driver_ee.R;
import com.ysyc.driver_ee.api.HttpResource;
import com.ysyc.driver_ee.api.NetworkCallback;
import com.ysyc.driver_ee.api.RetrofitClient;
import com.ysyc.driver_ee.base.BaseActivity;
import com.ysyc.driver_ee.constant.WebKeys;
import com.ysyc.driver_ee.tools.LogUtils;
import com.ysyc.driver_ee.tools.RegularExpression;
import com.ysyc.driver_ee.tools.ToastUtils;

import butterknife.BindView;
import io.reactivex.Observable;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

/**
 * <p>描述：(这里用一句话描述这个类的作用)</p>
 * 作者： Chan<br>
 * 日期： 2018/10/31 14:28<br>
 * 版本： v1.0<br>
 */
public class LoginActivity extends BaseActivity {
    @BindView(R.id.et_msg_view)
    EditText etMsgView;
    @BindView(R.id.tv_send_msg_view)
    TextView tvSendMsgView;
    @BindView(R.id.tv_view_terms_view)
    TextView tvViewTermsView;
    private NetworkCallback<Object> userInfoNetworkCallback;
    public static final String PHONE_NUMBER = "phone_number";
    private String phoneNumber;

    @Override
    protected int getLayoutId() {
        return R.layout.activity_login;
    }

    @Override
    protected void initView() {
        initNetworkCallback();
        etMsgView.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {
            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
                int length = s.toString().length();
                //删除数字
                if (count == 0) {
                    if (length == 4) {
                        etMsgView.setText(s.subSequence(0, 3));
                    }
                    if (length == 9) {
                        etMsgView.setText(s.subSequence(0, 8));
                    }
                }
                //添加数字
                if (count == 1) {
                    if (length == 4) {
                        String part1 = s.subSequence(0, 3).toString();
                        String part2 = s.subSequence(3, length).toString();
                        etMsgView.setText(part1 + " " + part2);
                    }
                    if (length == 9) {
                        String part1 = s.subSequence(0, 8).toString();
                        String part2 = s.subSequence(8, length).toString();
                        etMsgView.setText(part1 + " " + part2);
                    }
                }
            }

            @Override
            public void afterTextChanged(Editable s) {
                //将光标移动到末尾
                etMsgView.setSelection(etMsgView.getText().toString().length());
                //处理s
            }
        });
        String readAndAgree = "法律条款,";
        String protocol = "隐私条款";


        SpannableString spannableInfo = new SpannableString(readAndAgree);
        //第一个人(直男)的样式，点击事件
        spannableInfo.setSpan(new ClickableSpan() {
            @Override
            public void updateDrawState(TextPaint ds) {
                super.updateDrawState(ds);
                ds.setColor(Color.parseColor("#FF41C659"));//设置颜色
                ds.setUnderlineText(false);//去掉下划线
            }

            @Override
            public void onClick(View view) {
                Intent intent = new Intent(LoginActivity.this,WebViewActivity.class);
                intent.putExtra(WebViewActivity.TITLE_NAME,"法律条款");
                intent.putExtra(WebViewActivity.WEBVIEW_URL, WebKeys.LEGAL_NOTICES);
                startActivity(intent);
            }
        }, 0, readAndAgree.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);

        SpannableString spannableInfo1 = new SpannableString(protocol);
        //第一个人(直男)的样式，点击事件
        spannableInfo1.setSpan(new ClickableSpan() {
            @Override
            public void updateDrawState(TextPaint ds) {
                super.updateDrawState(ds);
                ds.setColor(Color.parseColor("#FF41C659"));//设置颜色
                ds.setUnderlineText(false);//去掉下划线
            }

            @Override
            public void onClick(View view) {
                Intent intent = new Intent(LoginActivity.this,WebViewActivity.class);
                intent.putExtra(WebViewActivity.TITLE_NAME,"隐私条款");
                intent.putExtra(WebViewActivity.WEBVIEW_URL, WebKeys.PRIVACY_POLICY);
                startActivity(intent);
            }
        },0, protocol.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
        tvViewTermsView.append(spannableInfo);
        tvViewTermsView.append(spannableInfo1);
        tvViewTermsView.setMovementMethod(LinkMovementMethod.getInstance());//开始响应点击事件
    }


    public void smsVerificationCode(View view) {
        LogUtils.e(etMsgView.getText().toString().replace(" ", ""));
        phoneNumber = etMsgView.getText().toString();
        String phoneNumberTrim = phoneNumber.replace(" ", "");
        if (RegularExpression.validatePhoneNumber(phoneNumberTrim)) {
            Observable<Object> userInfoObservable = RetrofitClient.getInstance().getHttpServices().getVerificationCode(phoneNumberTrim);
            userInfoNetworkCallback.postRequest(userInfoObservable, null);
//            Intent intent = new Intent(LoginActivity.this, VerificationCodeActivity.class);
//            intent.putExtra(PHONE_NUMBER, phoneNumber);
//            startActivity(intent);
        }else {
            ToastUtils.show("请输入正确手机号");
        }

    }

    private void initNetworkCallback() {
        userInfoNetworkCallback = new NetworkCallback<>(new HttpResource<Object>() {
            @Override
            public void onSuccess(Object object) {
                LogUtils.e("验证码发送成功");
                dismissDilog();
                Intent intent = new Intent(LoginActivity.this, VerificationCodeActivity.class);
                intent.putExtra(PHONE_NUMBER, phoneNumber);
                startActivity(intent);
            }

            @Override
            public void onFailure(String errorDes) {
                ToastUtils.show(errorDes);
                dismissDilog();
            }

            @Override
            public void onLoading(Dialog dialog) {
                showLoadingDialog();
            }
        });
    }





}
